package servlet;

import dao.AddressDao;
import entity.Address;
import entity.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet("/deleteAddress")
public class DeleteAddressServlet extends HttpServlet {
	
	@Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) 
            throws ServletException, IOException {
        doGet(request, response);
    }
	@Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) 
            throws ServletException, IOException {

		System.out.println("-----------deleteAddress------------");
		Integer addressId = Integer.valueOf(request.getParameter("addressId"));

		AddressDao dao = new AddressDao();
		int ret = dao.deleteToAddress(addressId);
		if(ret == -1) {
			response.sendRedirect("error.jsp");
			return;
		}else {
			response.setContentType("application/json");
			PrintWriter out = response.getWriter();
			out.write("{\"status\":\"success\"}");
			request.getRequestDispatcher("./selectAddress").forward(request, response);
		}

	}


}
